package com.service.impl;

import com.quartz.ScheduledJob;
import com.service.RefreshService;
import com.util.GetBeanInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
@Service
public class RefreshServiceImpl  implements RefreshService {
    @Autowired
    RedisTemplate redisTemplate;
    private static Logger logger = LoggerFactory.getLogger(ScheduledJob.class);
    @Override
    public void synBYBeanNames(List<String> beanNames) throws Exception{
        beanNames.stream().forEach(beanName->{
            try {
                Mapper mysqlMapper = (Mapper) new GetBeanInstance().getBean(beanName+"MysqlMapper");

                Mapper serverMapper= (Mapper) new GetBeanInstance().getBean(beanName+"SqlServerMapper");

                Class c=Class.forName("com.domain."+beanName.substring(0,1).toUpperCase()+beanName.substring(1,beanName.length()));
                //删除mysql旧数据
                mysqlMapper.delete(c.newInstance());
                //查询SqlServer数据
                serverMapper.selectAll().stream().forEach(temp->{
                    mysqlMapper.insert(temp);
                });
                logger.info("同步完成:"+beanName);
            }catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
